package com.demo.thread.demo_thread_create_way;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * .实现Runnable()接口定制执行目标（target）类，实现其run()方法
 * 推荐此方式。两个特点：
 * a.覆写Runnable接口实现多线程可以避免单继承局限
 * b.实现Runnable()可以更好的体现共享的概念
 * c.当执行目标类实现Runnable接口，此时执行目标（target）类和Thread是代理模式（子类负责真是业务的操作，thread负责资源调度与线程创建辅助真实业务。
 *
 * 无返回值
 *
 * @author peter
 * @date 2022/3/4 0:04
 */
public class Demo2 implements Runnable {
    public static AtomicInteger count = new AtomicInteger(10);
    @Override
    public void run() {
        while (count.get()>0) {
            int i = count.getAndDecrement();
            if (i > 0) {
                System.out.println(Thread.currentThread().getName()+"-当前剩余票数:"+i);
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Demo2 demo2 = new Demo2();
        Thread thread1 = new Thread(demo2, "线程1");
        Thread thread2 = new Thread(demo2, "线程2");
        Thread thread3 = new Thread(demo2, "线程3");
        thread1.start();
        thread2.start();
        thread3.start();

        Thread.sleep(1000);



    }
}
